package ui.swing.table.columnselection;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JTable;
import javax.swing.table.JTableHeader;

public class ColumnSelectableJTable extends JTable {

	private static final long serialVersionUID = -8812225136545546095L;

	public ColumnSelectableJTable(Object[][] items, Object[] colNames) {
		super(items, colNames);
		
		setColumnSelectionAllowed(true);
		setRowSelectionAllowed(false);
		
		// set up action listener on table header
		final JTableHeader header = getTableHeader();
		header.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				if(!e.isShiftDown()) {
					clearSelection();
					int pick = header.columnAtPoint(e.getPoint());
					addColumnSelectionInterval(pick, pick);
				}
			}
		});
	}
}
